package com.javafactroy.kunlong.base.config.datasource.oracle;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import com.javafactroy.kunlong.base.config.datasource.DatasourceConfig;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;


@Configuration
@Slf4j
@Data
@MapperScan(basePackages = {"com.javafactroy.kunlong.data.dao.oracle"}, sqlSessionTemplateRef = "oracleSqlSessionTemplate")
@ConditionalOnProperty(prefix = "spring.datasource.oracle", name = "enabled", havingValue = "true")
public class OracleDatasourceConfig extends DatasourceConfig {
	protected static String[] MYBATIS_MAPPER_LOCATIONS = new String[] {"classpath*:/mapper/oracle/*.xml"};

	protected static String MYBATIS_BASE_PACKAGE = "com.javafactroy.kunlong.data.dao.oracle";

	@Qualifier("oracleDataSource")
	@Bean(name = "oracleDataSource" )
	@ConfigurationProperties(prefix = "spring.datasource.oracle")
	public DataSource oracleDataSource() {
		DruidDataSource druidDataSource = new DruidDataSource();

		return druidDataSource;
	}

	@Bean("oracleSqlSessionTemplate")
	public SqlSessionTemplate buildSessionTemplate() throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory(oracleDataSource()));
	}

	@Bean(name = "oracleSessionFactroy")
	@Qualifier("oracleSessionFactroy")
	public SqlSessionFactory sqlSessionFactory(@Qualifier("oracleDataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		List<Resource> resList = new ArrayList<Resource>();
		for(String location:MYBATIS_MAPPER_LOCATIONS) {
			resList.addAll(Arrays.asList(resolver.getResources(location)));
		}
		bean.setMapperLocations(resList.toArray(new Resource[0]));
		//分页插件
		Interceptor interceptor = new PageInterceptor();
		Properties properties = new Properties();
		//分页数据库方言为mysql
		properties.setProperty("helperDialect", "oracle");
		//是否为分页数据库多个数据配置
		properties.setProperty("autoRuntimeDialect", "true");
		//是否分页合理化
		properties.setProperty("reasonable", "true");
		interceptor.setProperties(properties);

		SqlSessionFactory sessionFactory = bean.getObject();
		sessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
	   // HbatisSqlSessionDaoSupport.setHbatisSessionFactory(sessionFactory);
		return sessionFactory;
	}

	@Bean(name = "oracleTemplate")
	@Qualifier("oracleTemplate")
	public JdbcTemplate oracleTemplate(@Qualifier("oracleDataSource") DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}

	@Bean(name = "oracleTransactionManager")
	@Primary
	public DataSourceTransactionManager transactionManager(
			@Qualifier("oracleDataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}
}